
package com.blueberriesteam.kg.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.blueberriesteam.kg.entity.Queries;
import com.blueberriesteam.kg.R;
import com.blueberriesteam.kg.entity.Comment;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class MainActivity extends Activity {

    private ListView listView;
    private ArrayAdapter<String> stringArrayAdapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView)findViewById(R.id.List);

        URL url = null;
        try {
            url = new URL("http://www.kino-govno.com/comments/42484-postery-plan-pobega-i-sovetnik#comments");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        TagNode node = null;
        try {
            node = new HtmlCleaner().clean(url);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Document doc = null;
        try {
            doc = new DomSerializer(
                    new CleanerProperties()).createDOM(node);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        XPath xpath = XPathFactory.newInstance().newXPath();


        NodeList nodeList = null;
        try {
            nodeList = (NodeList)xpath.evaluate(Queries.COMMENT_VALUE,doc, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }




        NodeList nodeDate = null;
        try {
            nodeDate = (NodeList)xpath.evaluate(Queries.COMMENT_DATE,doc, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }


        NodeList nodeUser = null;
        try {
            nodeUser = (NodeList)xpath.evaluate(Queries.COMMENT_USER,doc, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        String[]s = new String[nodeList.getLength()];
        ArrayList<Comment> comments = new ArrayList<Comment>();
        for(int i = 0; i<nodeList.getLength(); i++){
            Comment comment = new Comment();
            comment.setUser(nodeUser.item(i).getFirstChild().getNodeValue());
            comment.setDate(nodeDate.item(i).getFirstChild().getNodeValue());
            comment.setString(nodeList.item(i).getFirstChild().getNodeValue());
            comments.add(comment);
            s[i] = nodeUser.item(i).getFirstChild().getNodeValue();


        }


;        String[] names = { "Иван", "Марья", "Петр", "Антон", "Даша", "Борис",
                "Костя", "Игорь", "Анна", "Денис", "Андрей" };
        stringArrayAdapter = new ArrayAdapter<String>(this,+
                android.R.layout.simple_list_item_1, s);

        // присваиваем адаптер списку
        listView.setAdapter(stringArrayAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater();
        return true;
    }

}
